package ru.javadevelop.vegetarian.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import ru.javadevelop.vegetarian.R;
import ru.javadevelop.vegetarian.common.ImageManager;
import ru.javadevelop.vegetarian.model.Image;
import ru.javadevelop.vegetarian.model.Recipe;

import java.util.ArrayList;


public class RecipesDataAdapter extends ArrayAdapter<Recipe> {

    int layoutResourceId;
    Context _context;

    private ArrayList<Recipe> recipes;
    //******************************************************************************************************************
    // Конструктор
    public RecipesDataAdapter(Context context,  int layoutResourceId, ArrayList<Recipe> recipes) {
        super(context, layoutResourceId, recipes);
        this.recipes = recipes;
        this.layoutResourceId = layoutResourceId;
        this._context = context;
    }
    //******************************************************************************************************************
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null)
        {
            LayoutInflater inflater = (LayoutInflater)_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(layoutResourceId, parent, false);
        }
        if (recipes.size()>0)
        {
            TextView recipeName = (TextView)convertView.findViewById(R.id.recipeName);
            TextView recipeIngredients = (TextView)convertView.findViewById(R.id.recipeIngredients);
            ImageView small_image = (ImageView)convertView.findViewById(R.id.small_image);
            ArrayList<Image> images = recipes.get(position).getImages();
            if ( (images!=null)&&(images.size()>0) )
            {
                Image  image = images.get(0);
                ImageManager im = new ImageManager(_context, 150, 110);
                Bitmap bm = im.setIsScale(true)
                        .setIsResize(true)
                        .setIsCrop(true)
                        .getFromFile(image.getFilePath(_context));
                small_image.setImageBitmap(bm);
            }
            else
            {
                 small_image.setImageDrawable(_context.getResources().getDrawable(R.drawable.img_no_image));
            }
            recipeName.setText(recipes.get(position).name);
            recipeIngredients.setText(recipes.get(position).ingredients);
        }
        return (convertView);
    }
    //******************************************************************************************************************
    // возвращает содержимое выделенного элемента списка
    public Recipe GetItem(int position) {
        return recipes.get(position);
    }

}
